package com.js.data.system.controller;


import com.js.data.common.api.CommonResult;
import com.js.data.common.dto.UserMenuVO;
import com.js.data.system.annotation.Log;
import com.js.data.system.dto.MenuDTO;
import com.js.data.system.vo.CommonTreeVO;
import com.js.data.system.vo.MenuDetailVO;
import com.js.data.system.vo.MenuTreeVO;
import com.js.data.system.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * <p>
 * 系统菜单 前端控制器
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */

@RestController
@RequestMapping("/sys")
public class MenuController {

    @Resource
    private MenuService menuService;


    /**
     * 菜单列表
     *
     * @param parentId 父id
     * @return List<MenuListVO>
     */
    @PreAuthorize("@perm.check('system:menu:tree')")
    @GetMapping(value = "/menu/{parentId}/tree")
    public CommonResult<List<MenuTreeVO>> treeMenu(@PathVariable Long parentId) {
        return CommonResult.success(menuService.treeMenu(parentId));
    }


    @GetMapping(value = "/menu/simple", produces = "application/json; charset=utf-8")
    public CommonResult<List<MenuTreeVO>> treeSimpleMenu() {
        return CommonResult.success(menuService.treeSimpleMenu());
    }

    /**
     * 新增菜单
     *
     * @param menuDTO 菜单表单
     * @return void
     */
    @Log(value = "新增菜单")
    @PostMapping(value = "/menu")
    @PreAuthorize("@perm.check('system:menu:add')")
    public CommonResult<Void> saveMenu(@Validated @RequestBody MenuDTO menuDTO) {
        menuService.saveMenu(menuDTO);
        return CommonResult.success();
    }

    /**
     * 修改菜单
     *
     * @param id      菜单id
     * @param menuDTO 菜单表单
     * @return void
     */
    @Log(value = "修改菜单")
    @PutMapping(value = "/menu/{id}")
    @PreAuthorize("@perm.check('system:menu:edit')")
    public CommonResult<Void> editMenu(@PathVariable Long id, @Validated @RequestBody MenuDTO menuDTO) {
        menuService.editMenu(id, menuDTO);
        return CommonResult.success();
    }

    /**
     * 删除菜单
     *
     * @param id 菜单id
     * @return void
     */
    @Log(value = "删除菜单")
    @DeleteMapping(value = "/menu/{id}")
    @PreAuthorize("@perm.check('system:menu:del')")
    public CommonResult<Void> deleteMenu(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return CommonResult.success();
    }

    /**
     * 获取菜单详情
     *
     * @param id id
     * @return MenuDetailVO
     */
    @GetMapping(value = "/menu/{id}")
    public CommonResult<MenuDetailVO> getMenu(@PathVariable Long id) {
        return CommonResult.success(menuService.getMenu(id));
    }


    /**
     * 当前登录用户菜单
     *
     * @return List<UserMenuVO>
     */
    @GetMapping(value = "/menu/user")
    public CommonResult<Set<UserMenuVO>> userMenu() {
        return CommonResult.success(menuService.userMenu());
    }


    /**
     * 应用菜单树
     * @return List<CommonTreeVO>
     */
    @GetMapping(value = "/menu/tree", produces = "application/json; charset=utf-8")
    public CommonResult<List<CommonTreeVO>> treeAppMenu() {
        return CommonResult.success(menuService.treeAppMenu());
    }
}
